<?php
namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\city;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use PhpParser\Node\Stmt\Echo_;


class CityController extends Controller
{
    //增删改查
    public function test()
    {
//        $city = DB::select('select * from city');
//        var_dump($city);

//        $bool = DB::insert('insert into city(id,name) values (?,?)',[4,'河南']);
//        var_dump($bool);

//        $num = DB::update('update city set name = ? where id = 4',['上海']);
//        var_dump($num);
//        $num = DB::delete('delete from city where id = 4');
//        var_dump($num);

        return '增删改查';
    }

    //查询构造器插入数据
    public function query1()
    {
//        $bool = DB::table('city')->insert(
//            ['name'=>'上海']
//        );
//        var_dump($bool);

        //获取insert的数据id
//        $id = DB::table('city')->insertGetId(
//            ['name'=>'福建','num_people'=>200]
//        );
//        var_dump($id);
        $bool = db::table('city')->insert(
            [
                ['name'=>'山西'],
                ['name'=>'山东']
            ]
        );
        var_dump($bool);
    }

    //查询构造器修改数据
    public function query2()
    {
//       $num = DB::table('city')
//           ->where('id','>',14)
//           ->update(['name'=>'西藏','num_people'=>244]);
//       var_dump($num);


//        $num = DB::table('city')->where('id','>=',17)->increment('num_people');//自增1
        $num = DB::table('city')->where('id','>=',17)->increment('num_people',12);//自增12
        //        $num = DB::table('city')->where('id','>=',25)->decrement('num_people');//自减3
//        $num = DB::table('city')->where('id','>=',25)->decrement('num_people',3);//自减3
       // $num = DB::table('city')->where('id','=',7)->decrement('num_people',3,['name'=>'山西']);//自减3
        var_dump($num);

    }
    //查询构造器删除数据
    public function query3()
    {
        DB::table('city')->where('id',7)->delete();

//        DB::table('city')->truncate();//整表删除

    }
    public function query4()
    {
        //get

//        $city = DB::table('city')->get();//取整表
        //first
//        $city = DB::table('city')->first();//取第一tiao
//        $city = DB::table('city')->orderBy('id','desc')->first();//倒序取第一条

        //where
//        $city = DB::table('city')->where('id','>=',20)->get();
        $city = DB::table('city')->whereraw('id>=? and name=?',[20,'山西'])->get();

        //pluck
//        $city = DB::table('city')->whereraw('id>=? and name=?',[20,'山西'])->pluck('num_people');
        //lists  -- Illuminate\Database\Query\Builder::lists does not exist.
//        $city = DB::table('city')->lists('name','id');

        //select
//        $city = DB::table('city')->select('id','name')->get();


        //chuck
        echo '<pre>';
        DB::table('city')->orderBy('id')->chunk(2,function($city){
            var_dump($city);
        });
//        dd($city);
    }

    public function query5()
    {
//        $num = DB::table('city')->count();
//        $num = DB::table('city')->max('num_people');
//        $num = DB::table('city')->min('num_people');
//        $num = DB::table('city')->avg('num_people');
        $num = DB::table('city')->sum('num_people');
        var_dump($num);
    }

    public function orm1()
    {
        //all()
//        $city = city::all();
        //find()
//        $city = city::find(20);
        //findorfail()
//        $city = city::findorfail(30);

        //
//        $city = city::where('id','>','6')
//            ->orderby('num_people','desc')
//            ->first();
//        $city = city::where('id','>','6')
//            ->orderby('num_people','desc')
//            ->get();
//        $city = city::where('id','>','1')
//            ->orderby('id','desc')
//            ->chunk(2,function ($city){
//                var_dump($city);
//            });
//        dd($city);

        //聚合函数
        $max = city::where('id', '>=',5)->max('num_people');
        var_dump($max);
    }

    public function orm2()
    {
//        $city = new city();
//        $yy = $city->all();
//        dd($yy);

//        $city->name = '陕西';
//        dd($city);
//        $bool = $city->save();

        //使用模型的create方法新增数据
//         $city = city::create(
//            ['name'=>'湖南','num_people'=>124]
//        );

        //         $city = city::insert(
//            ['name'=>'湖南','num_people'=>124]
//        );

          // firstOrCreate() -- 查询,如果没有创建
//        $city = city::firstOrCreate(
//            ['name' => '河南']
//        );

        // firstOrNew() -- 查询,如果没有创建实例
        $city = city::firstOrNew(
            ['name' => '广州']
        );
        $city->save();
        dd($city);


    }

    public  function orm3()
    {
//        $city = city::find(20);
//        $city->name = '广西';
//        $bool = $city -> save();
//        var_dump($bool);
         $num = city::where('id','>','150')
             ->update(['name'=>'上海']);
         var_dump($num);


    }
    public  function orm4()
    {
        //通过模型删除
//        $city = city::find(21);
//        $bool = $city->delete();
//        var_dump($bool);
        //通过主键删除
//        $num = city::destroy(22);
//        $num = city::destroy([25,26,27,32,33]);

//        $num = city::where('id','>',40)
//            ->delete();


//        $bool = city::find(40)->delete();
//        var_dump($bool);



//        city::insert(
//            [
//                ['name'=>'山西'],
//                ['name'=>'山东']
//            ]
//        );
//         $city = city::whereraw('id>0 and id <53')->delete();


        $city = city::where('name', '=','山西')->delete();
        var_dump($city);
    }

    public function request1(Request $request)
    {
        //1.取值
//        echo $request->input('name');
//        echo $request->input('name','位置');

//        if ($request->has('name'))
//        {
//            echo $request->input('name');
//        }else
//        {
//            echo 'name无参数';
//        }

        //取出所有参数
//        dd($request->all());

        //2.判断请求类型
//        echo  $request->method();
//        if ($request->isMethod('get'))
//        {
//            echo 'get';
//        }else
//        {
//            echo 'no get';
//        }

//        $res = $request->is('api/*');//满足某种规则的请求
//        var_dump($res);

//        echo $request->url();//获取请求的url


    }

    public function session1(Request $request){

        //1. http request session()
//        $request->session()->put('key','value');
//        echo $request->session()->get('key');
        //2. session()
//        session()->put('key1','value1');
//        echo session()->get('key1');
        // 3.Session
//        Session::put('key','jjj');//存贮数据
//        echo Session::get('key');//获取
//
//        echo Session::get('key4','default');//默认值
//
//        Session::put(['key4'=>'value4']);//数组形势存储数据
//        echo Session::get('key4');

        //把数据放到session的数组中
//        Session::push('city','上海');
//        Session::push('city','北京');
//        var_dump(Session::get('city','default'));//取出数据
//        var_dump(Session::pull('city','default'));//取出数据并删除
//        dd(Session::all());//取出所有值
//        Session::has('key1');//判断是否存在
//        Session::forget('key1');//删除key1
//        Session::flush();//删除session所有数据
//        Session::flash('key','dsf');//暂存数据,只有第一次获取时存在

        return Session::get('msg','暂无数');
    }


    public function response()
    {
        //3.响应json
//        $data = [
//            'errCode' => 0,
//            'errMsg' => 'suc',
//            'data' => [
//                'list' => [
//                    ['title'=>'北京','subTitle'=>'北京市'],
//                    ['title'=>'上海','subTitle'=>'上海市'],
//                ],
//                'num' => 2,
//            ]
//
//        ];
//        return response()->json($data);

        //4.重定向
//        return redirect('api/session1');
//        return redirect('api/session1')->with('msg','dsdsd');

//        return redirect()->action('Home\CityController@session1')->with('msg','我是快闪数据');

//        return redirect()->route('session1');

        return redirect()->back();
    }

    public function activity0()
    {
        return '活动快要开始';
    }
    public function activity1()
    {
        return '活动进行中';
    }
    public function activity2()
    {
        return '活动进行中';
    }


    public function upload()
    {
        return 'shangchuan';
    }




}